﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ComponentModel;
using System.Collections.Generic;

using SBPweb.Messenger.Clients.Base;

namespace SBPweb.Messenger.Clients.WLM.Connections
{

    public class ConnectionManager : DuplexServiceConnectorBase, IAsyncConnectionManager
    {

        #region Private fields

        private bool isOnline = false;

        #endregion

        #region Constructor logic

        public ConnectionManager()
            : base("http://localhost/SBPweb.Messenger.Host/Services/WLM/ConnectionManager.svc")
        {
        }

        #endregion

        #region IAsyncConnectionManager Members

        public void Order(Message message)
        {
        }

        public void EnumeratePresenceStatesAsync()
        {
            Message msg = Message.CreateMessage(Channel.GetProperty<MessageVersion>(), "SBPweb.Messenger.WLM/IConnectionManager/Order", string.Empty);
            Channel.Send(msg);
        }

        public event EventHandler<ResultedAsyncCompletedEventArgs<List<string>>> EnumeratePresenceStatesCompleted;

        public void SetPresenceStateAsync(string state)
        {
            throw new NotImplementedException();
        }

        public event EventHandler<AsyncCompletedEventArgs> SetPresenceStateCompleted;

        public void ConnectAsync(string userName, string password)
        {
            throw new NotImplementedException();
        }

        public event EventHandler<AsyncCompletedEventArgs> ConnectCompleted;

        #endregion

        #region IConnectionManager Members

        public bool IsOnline
        {
            get
            {
                return isOnline;
            }
        }

        public List<string> EnumeratePresenceStates()
        {
            throw new NotImplementedException();
        }

        public void SetPresenceState(string state)
        {
            throw new NotImplementedException();
        }

        public void Connect(string userName, string password)
        {
            throw new NotImplementedException();
        }

        #endregion

    }

    /*
    public class ConnectionManager : Services.Connections.ConnectionManagerClient, IAsyncConnectionManager
    {

        #region Private fields

        private bool isOnline = false;

        #endregion

        #region Constructor logic

        public ConnectionManager()
            : base()
        {
            base.EnumeratePresenceStatesCompleted += new EventHandler<Services.Connections.EnumeratePresenceStatesCompletedEventArgs>(ConnectionManager_EnumerateStatesCompleted);
        }

        #endregion

        #region Property accessors

        public bool IsOnline
        {
            get
            {
                return isOnline;
            }
        }

        #endregion

        #region Events

        public new event EventHandler<ResultedAsyncCompletedEventArgs<List<string>>> EnumeratePresenceStatesCompleted;

        #endregion

        #region Methods

        public List<string> EnumeratePresenceStates()
        {
            throw new NotSupportedException("Please use the Asynchronous methods!");
        }

        public void SetPresenceState(string state)
        {
            base.SetPresenceStateAsync(state);
        }

        public void Connect(string userName, string password)
        {
            base.ConnectAsync(userName, password);
        }

        #endregion

        #region Event handlers

        private void ConnectionManager_EnumerateStatesCompleted(object sender, Services.Connections.EnumeratePresenceStatesCompletedEventArgs e)
        {
            if (this.EnumeratePresenceStatesCompleted != null)
            {
                this.EnumeratePresenceStatesCompleted(this, new ResultedAsyncCompletedEventArgs<List<string>>(e.Error, e.Cancelled, e.UserState, new List<string>(e.Result)));
            }
        }

        #endregion

    }
    */

}